//
//  KZSoundTool.m
//  听说弱点强化训练
//
//  Created by 朱凯 on 2018/10/29.
//  Copyright © 2018年 盛超. All rights reserved.
//

#import "KZSoundTool.h"
#import <objc/runtime.h>

@implementation KZSoundTool

void soundCompleteCallback(SystemSoundID soundID,void * clientData){
    AudioServicesDisposeSystemSoundID(soundID);
}

+ (void)stop {
    
    SystemSoundID soundID = (SystemSoundID)[objc_getAssociatedObject(self, "KZSoundID") unsignedIntegerValue];
    AudioServicesRemoveSystemSoundCompletion(soundID);
    AudioServicesDisposeSystemSoundID(soundID);
}

+ (void)playSound:(KZSoundType)type {
    
    NSString *soundPath = [[NSBundle mainBundle] pathForResource:[self soundName:type] ofType:@"caf"];
    NSURL *soundUrl = [NSURL fileURLWithPath:soundPath];
    SystemSoundID soundID = 0;
    AudioServicesCreateSystemSoundID((__bridge CFURLRef)(soundUrl), &soundID);
    AudioServicesAddSystemSoundCompletion(soundID, NULL, NULL, soundCompleteCallback, NULL);
    AudioServicesPlaySystemSound(soundID);
    
    
    objc_setAssociatedObject(self, "KZSoundID", @(soundID), OBJC_ASSOCIATION_COPY);
}

+ (NSString *)soundName:(KZSoundType)type {
    
    return @[@"回答正确",
             @"回答错误",
             @"结果-成功",
             @"结果-失败",
             @"首页分析",
             @"三关",
             @"翻页"
             ][(int)type];
}


@end
